package com.itsc.action;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itsc.entities.CenterImageDes;
import com.itsc.service.inter.ICenterImageDesService;
import com.itsc.util.HttpUtil;
import com.itsc.util.page.Page;
import com.itsc.util.page.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class CenterImageDesAction implements ModelDriven<CenterImageDes>{

	private static final String PATHURL = "./uploadcenterImages";
	private CenterImageDes model = new CenterImageDes();
	private ICenterImageDesService centerImageDesService;
	String url = null;
	private List<Integer> check_id;
	
	private String searchname;// 查询条件
	/* 分页显示数据 */
	private int curPage = 1;// 当前页
	private int pageSize = 2;// 每页显示条数
	private int countPage;// 总页数
	
	//文件上传属性
	private File upload;//具体上传文件的 引用 , 指向临时目录中的临时文件 
	private String uploadFileName;//上传文件的名字,FileName 固定的写法 
	private String uploadContentType;//上传文件的类型， ContentType 固定的写法
	
	public List<Integer> getCheck_id() {
		return check_id;
	}
	public void setCheck_id(List<Integer> check_id) {
		this.check_id = check_id;
	}
	public String getSearchname() {
		return searchname;
	}
	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCountPage() {
		return countPage;
	}
	public void setCountPage(int countPage) {
		this.countPage = countPage;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public void setCenterImageDesService(
			ICenterImageDesService centerImageDesService) {
		this.centerImageDesService = centerImageDesService;
	}
	@Override
	public CenterImageDes getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public String list() throws Exception{
		int count = centerImageDesService.getCount();
		PageUtils.initParamter(count, pageSize);
		Page page = PageUtils.getPage();
		List<CenterImageDes> centerList = centerImageDesService.getPage(page);
		ActionContext.getContext().put("centerList", centerList);
		ActionContext.getContext().put("page", page);
		return "list";
	}
	
	public String addModel() throws Exception{
		return "insert";
	}

	public String add() throws Exception{
		
		int i=uploadFileName.lastIndexOf(".");//原名称里倒数第一个"."在哪里 
		String ext=uploadFileName.substring(i);//取得后缀，及"."后面的字符
		
		String uploadName = UUID.randomUUID().toString()+ext;
		//根据服务器的文件保存地址和原文件名创建目录文件全路径
        String path = ServletActionContext.getServletContext().getRealPath("/uploadcenterImages");
		//生成上传的File对象
        File targetFile = new File(path,uploadName);
        
        //复制File对象，从而实现上传文件
        FileUtils.copyFile(upload, targetFile);
        
        model.setCid_url(PATHURL+"/"+uploadName);
        
		centerImageDesService.save(model);
		return "ok";
	}
	
	public String delete() throws Exception {
		centerImageDesService.delete(model);
		return "ok";
	}
	
	public String moredelete() throws Exception{
		for(Integer id : check_id){
			CenterImageDes centerImageDes = centerImageDesService.selectById(id);
			centerImageDesService.delete(centerImageDes);
		}
		
		return "ok";
	}
	
	public String edit() throws Exception{
		
		CenterImageDes center = centerImageDesService.selectById(model.getCid_id());
		ActionContext.getContext().getValueStack().push(center);
		url = center.getCid_url();
		return "edit";
	}
	
	public String update() throws Exception{
		if(upload==null){
			model.setCid_url(url);
		}else{
			int i=uploadFileName.lastIndexOf(".");//原名称里倒数第一个"."在哪里 
			String ext=uploadFileName.substring(i);//取得后缀，及"."后面的字符
			
			String uploadName = UUID.randomUUID().toString()+ext;
			//根据服务器的文件保存地址和原文件名创建目录文件全路径
	        String path = ServletActionContext.getServletContext().getRealPath("/uploadcenterImages");
			//生成上传的File对象
	        File targetFile = new File(path,uploadName);
	        
	        //复制File对象，从而实现上传文件
	        FileUtils.copyFile(upload, targetFile);
	        
	        model.setCid_url(PATHURL+"/"+uploadName);
		}
		centerImageDesService.update(model);
		return "ok";
	}
	
	public String search() throws Exception {
		List<CenterImageDes> list = null;
		Page page = null;
		if (searchname == null && searchname.equals("")) {
			int count = centerImageDesService.getCount();
			PageUtils.initParamter(count, pageSize);
			page = PageUtils.getPage();
			if (curPage <= 0) {
				curPage = 1;
			} else if (curPage > page.getTotalPage()) {
				curPage = page.getTotalPage();
			} else {
			}
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = centerImageDesService.search(searchname, page);
		} else {
			int count = centerImageDesService.getCount(searchname);
			PageUtils.initParamter(count, pageSize);
			PageUtils.setCurrentPage(curPage);
			page = PageUtils.getPage();
			list = centerImageDesService.search(searchname, page);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("page", page);
		String js = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
		HttpUtil.rendText(js);
		return null;
	}
	public String checkDepartment() throws Exception{
		
		Boolean flag = centerImageDesService.checkDepartment(model.getCid_name());
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			
			ServletActionContext.getResponse().getWriter().print("0");
		}

		return null;
	}
	
	public String checkDep() throws Exception{
		Boolean flag = centerImageDesService.checkDepartment(model.getCid_name());
		Integer id = centerImageDesService.checkIdByDepartment(model.getCid_name());
		
		if (flag) {

			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			if(id==model.getCid_id()){
				ServletActionContext.getResponse().getWriter().print("1");
			}else{
				ServletActionContext.getResponse().getWriter().print("0");
			}
		}

		return null;
	}
	
	public String show() throws Exception{
		CenterImageDes center = centerImageDesService.selectById(model.getCid_id());
		ActionContext.getContext().getValueStack().push(center);
		
		return "show";
	}
}
